package com.sskj.mine.ui.activity;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.LazyLoaders;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.lib.util.PatternUtils;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.util.SendCodeUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.SettingFundPwdPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;


@Route(path = RConfig.MINE_SET_FUND_PWD)
public class SettingFundPwdActivity extends BaseActivity<SettingFundPwdPresenter> {
    @Autowired
    int type = 0;//1 修改资金密码 0 设置资金密码
    @BindView(R2.id.left_tv)
    TextView left_tv;
    @BindView(R2.id.code_edt)
    EditText code_edt;
    @BindView(R2.id.etFundPwd)
    EditText etFundPwd;
    @BindView(R2.id.submit_btn)
    Button submit_btn;
    @BindView(R2.id.get_code_tv)
    TextView get_code_tv;
    @BindView(R2.id.mobile_edt)
    EditText mobile_edt;
    @BindView(R2.id.type_name)
    TextView type_name;
    @BindView(R2.id.etFundPwdAgain)
    TextView etFundPwdAgain;
    @BindView(R2.id.ivassetpwd1)
    ImageView ivassetpwd1;
    @BindView(R2.id.ivassetpwd2)
    ImageView ivassetpwd2;
    @Inject
    UserViewModel userViewModel;
    private ToastNextInputs inputs;
    private boolean isSee;
    private boolean isSee1;
    private Disposable timeDispo;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_content_setting_fund_pwd;
    }

    @Override
    public SettingFundPwdPresenter getPresenter() {
        return new SettingFundPwdPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        if (type == 0) {
            setTitle(App.INSTANCE.getString(R.string.minesettingFundPwdActivity1));
        } else {
            setTitle(App.INSTANCE.getString(R.string.minesettingFundPwdActivity2));
        }
        inputs = new ToastNextInputs();
        LazyLoaders lazyLoaders = new LazyLoaders(this);
        inputs
                .add(etFundPwd, SchemeUtil.notEmpty(etFundPwd), SchemeUtil.pwd())
                .add(etFundPwdAgain, SchemeUtil.notEmpty(etFundPwdAgain), SchemeUtil.equalsPwd(lazyLoaders.fromEditText(etFundPwd)))
                .add(code_edt, SchemeUtil.notEmpty(code_edt), SchemeUtil.smsCode());
        //手机号
        if (PatternUtils.isMobile(SPUtil.get(SPConfig.USER_ACCOUNT, ""))) {
            mobile_edt.setText(HideUtil.getPhoneHide(SPUtil.get(SPConfig.USER_ACCOUNT, "")));
        } else if (PatternUtils.isMobile(SPUtil.get(SPConfig.USER_ACCOUNT, ""))) {
            //邮箱
            mobile_edt.setText(HideUtil.getEmialAddress(SPUtil.get(SPConfig.USER_ACCOUNT, "")));
        } else {
            mobile_edt.setText(SPUtil.get(SPConfig.USER_ACCOUNT, ""));
        }

        if (SPUtil.get(SPConfig.USER_ACCOUNT, "").contains("@")) {
            type_name.setText(App.INSTANCE.getString(R.string.minesettingFundPwdActivity3));
            code_edt.setHint(App.INSTANCE.getString(R.string.minewithdrawActivity2));
        } else {
            type_name.setText(App.INSTANCE.getString(R.string.minesettingFundPwdActivity5));
            code_edt.setHint(App.INSTANCE.getString(R.string.minesettingFundPwdActivity6));
        }

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(200, get_code_tv, () -> {
            if (TextUtils.isEmpty(etFundPwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineaddPaymentActivity19));
                return;
            }
            if (TextUtils.isEmpty(etFundPwdAgain.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minesettingFundPwdActivity8));
                return;
            }
            if (!etFundPwd.getText().toString().equals(etFundPwdAgain.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minesettingFundPwdActivity9));
                return;
            }
            mPresenter.sendCode(SPUtil.get(SPConfig.USER_ACCOUNT, ""));
        });

        ClickUtil.click(200, ivassetpwd1, () -> {
            isSee = !isSee;
            etFundPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivassetpwd1.setImageResource(isSee ? R.mipmap.mine_icon_show : R.mipmap.mine_icon_hide);

        });
        ClickUtil.click(200, ivassetpwd2, () -> {
            isSee1 = !isSee1;
            etFundPwdAgain.setTransformationMethod(isSee1 ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivassetpwd2.setImageResource(isSee1 ? R.mipmap.mine_icon_show : R.mipmap.mine_icon_hide);

        });
        ClickUtil.click(submit_btn, () -> {
            if (inputs.test()) {
                mPresenter.setFundPwd(code_edt.getText().toString(), etFundPwd.getText().toString());
            }
        });
    }

    public void onSendCodeSuccess() {
        startCount();
    }

    public void startCount() {
        get_code_tv.setEnabled(false);
        DisposUtil.close(timeDispo);
        timeDispo = SendCodeUtil.timeCount(this, get_code_tv);
    }

    public void onSetSuccess() {
        userViewModel.update();
        finish();
    }

}
